//
//  CategoryViewController.m
//  App-MyFairldy
//
//  Created by 客官大大 on 15/5/23.
//  Copyright (c) 2015年 客官大大. All rights reserved.
//

#import "CategoryViewController.h"

@interface CategoryViewController ()

@end

@implementation CategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activity.color = [UIColor blackColor];
    activity.center = self.view.center;
    [self.view addSubview:activity];
    [activity startAnimating];
    [self downLoadThisPageData];
    
    [activity release];
}
- (void)initView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(50, 50);
    layout.minimumLineSpacing = 60;
    layout.minimumInteritemSpacing = 50;
    layout.sectionInset = UIEdgeInsetsMake(50, 30, 30, 30);
    
    mCollect = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 64) collectionViewLayout:layout];
    mCollect.delegate = self;
    mCollect.dataSource = self;
    [mCollect registerClass:[CategoryCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    mCollect.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:mCollect];
    
    
}

- (void)downLoadThisPageData
{
    NSString *str = @"http://food.boohee.com/fb/v1/categories?kind=group";
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:str];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
    request.HTTPMethod = @"GET";
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        self.dataArray = [NSMutableArray array];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"categories"];
        for (NSDictionary *dic1 in array) {
            
            CategoryModel *model = [CategoryModel getCategoryModel];
            model.name = [dic1 objectForKey:@"name"];
            NSString *str1 = [dic1 objectForKey:@"image_url"];
            NSURL *url = [NSURL URLWithString:str1];
            NSData *data = [NSData dataWithContentsOfURL:url];
            model.image_url = [UIImage imageWithData:data];
            [self.dataArray addObject:model];
            [activity stopAnimating];
            [self initView];
        }
    }];
}

#pragma mark UICollectionView代理方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    CategoryModel *model = [self.dataArray objectAtIndex:indexPath.row];
    cell.iconImage.image = model.image_url;
    cell.backgroundColor = [UIColor whiteColor];
    cell.label1.text = model.name;
    cell.label1.font = [UIFont systemFontOfSize:12];
    NSLog(@"%@", cell.label1.text);
    
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
    
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CategoryFirstViewController *first = [[CategoryFirstViewController alloc] init];
    [self.navigationController pushViewController:first animated:YES];
    [first release];
}
- (void)dealloc
{
    [_dataArray release];
    [super dealloc];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
